package com.ruoyi.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.OperatorRecord;
import com.ruoyi.web.domain.Product;
import com.ruoyi.web.service.IOperatorRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 派单Controller
 * 
 * @author song
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/admin/operator")
public class OperatorRecordController extends BaseController
{
    @Autowired
    private IOperatorRecordService operatorRecordService;

    /**
     * 查询派单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:list')")
    @GetMapping("/list")
    public TableDataInfo list(OperatorRecord operatorRecord)
    {
        startPage();
        List<OperatorRecord> list = operatorRecordService.selectOperatorRecordList(operatorRecord);
        return getDataTable(list);
    }

    /**
     * 导出派单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:export')")
    @Log(title = "派单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperatorRecord operatorRecord)
    {
        List<OperatorRecord> list = operatorRecordService.selectOperatorRecordList(operatorRecord);
        ExcelUtil<OperatorRecord> util = new ExcelUtil<OperatorRecord>(OperatorRecord.class);
        util.exportExcel(response, list, "派单数据");
    }

    /**
     * 获取派单详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(operatorRecordService.selectOperatorRecordById(id));
    }

    /**
     * 新增派单
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:add')")
    @Log(title = "派单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperatorRecord operatorRecord)
    {
        return toAjax(operatorRecordService.insertOperatorRecord(operatorRecord));
    }

    /**
     * 修改派单
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:edit')")
    @Log(title = "派单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperatorRecord operatorRecord)
    {
        return toAjax(operatorRecordService.updateOperatorRecord(operatorRecord));
    }

    /**
     * 删除派单
     */
    @PreAuthorize("@ss.hasPermi('admin:operator:remove')")
    @Log(title = "派单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(operatorRecordService.deleteOperatorRecordByIds(ids));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OperatorRecord> util = new ExcelUtil<>(OperatorRecord.class);
        List<OperatorRecord> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = operatorRecordService.importProduct(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<OperatorRecord> util = new ExcelUtil<>(OperatorRecord.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
